//
//  PywGetSMSCodeView.m
//  PywSdk
//
//  Created by Jade on 16/8/30.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywGetSMSCodeView.h"
#import "PywTextFieldView.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "UIImage+Pyw.h"
#import "UILabel+Pyw.h"
#import "PywMasonry.h"
#import "PywGetSmsCodeApi.h"
#import "PywCommonManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywSdk+login.h"
#import "UILabel+PywAttributeTextTapAction.h"
#import "MutableDictionary.h"
#import "PywBuriedManager.h"

@interface PywGetSMSCodeView()<PywAttributeTapActionDelegate>
@property (nonatomic, strong) PywTextFieldView  *phoneTextView; // 手机号码输入框
@property (nonatomic, strong) UIButton          *validCodeBtn;  // 获取验证码按钮
@property (nonatomic, strong) UILabel           *reminderLabel; // 温馨提示Label

@property (nonatomic, strong) PywGetSmsCodeApi  *getSmsCodeApi; // 获取验证码请求
@property (nonatomic, copy) NSString            *smsType;       // 获取验证码场景类型
@end

@implementation PywGetSMSCodeView

- (instancetype)initWithSmsType:(NSInteger)smsType
{
    self = [super init];
    if (self) {
        self.smsType = [NSString stringWithFormat:@"%ld",(long)smsType];
        [self setUp];
    }
    return self;
}

#pragma mark - setUp
- (void)setUp
{
    [self addSubview:self.phoneTextView];
    [self addSubview:self.validCodeBtn];
    
    [self addConstraints];
}

- (void)addConstraints
{
    WEAK_SELF;
    [self.phoneTextView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.mas_top).offset(80);
        make.width.equalTo(weakSelf.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    
    [self.validCodeBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.phoneTextView.mas_bottom).offset(40);
        make.width.height.equalTo(weakSelf.phoneTextView);
    }];
    
    if ([self.smsType integerValue] == SMSCODETYPE_NOTLOGIN_FORGETPWD
        ||[self.smsType integerValue] == SMSCODETYPE_LOGIN_FORGETPWD) {
        [self addSubview:self.reminderLabel];
        [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(weakSelf.validCodeBtn.mas_bottom).offset(20);
        }];
    }
    
}

#pragma mark - getter
- (PywTextFieldView *)phoneTextView
{
    if (!_phoneTextView) {
        _phoneTextView = [PywTextFieldView phoneTextFieldWithLeftImg:@"icon_phone_normal"
                                                     TextPlaceholder:@"请输入您的手机号码"];
        [_phoneTextView setAccessibilityId:@"binphone_phone"];
    }
    return _phoneTextView;
}
- (UIButton *)validCodeBtn
{
    if (!_validCodeBtn) {
        _validCodeBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                               cornerRadius:20
                                                 titleColor:[UIColor whiteColor]
                                         titleLabelFontSize:18
                                                      title:@"获取验证码"];
        
        [_validCodeBtn addTarget:self action:@selector(getValidCodeCilck) forControlEvents:UIControlEventTouchUpInside];
        [_validCodeBtn setAccessibilityIdentifier:@"binphone_getcode"];
    }
    return _validCodeBtn;
}
- (UILabel *)reminderLabel
{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:nil
                                  textAlignment:NSTextAlignmentCenter
                                       fontSize:12
                                      textColor:[UIColor blackColor]];
        
        _reminderLabel.attributedText = [self labelAttributedText];
        [_reminderLabel pyw_addAttributeTapActionWithStrings:@[@"联系客服"] delegate:self];
    }
    return _reminderLabel;
}

- (NSMutableAttributedString *)labelAttributedText
{
    NSString *str = [NSString stringWithFormat:@"温馨提示：如游戏账号未绑定手机，请联系客服申诉"];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:12];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(0, str.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(str.length - 6, 4)];
    // 添加下划线
    [attributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(str.length - 6, 4)];
    // 设置行高
    NSMutableParagraphStyle *sty = [[NSMutableParagraphStyle alloc] init];
    sty.lineSpacing = 5;
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:sty range:NSMakeRange(0, str.length)];
    return attributedStr;
}

- (PywGetSmsCodeApi *)getSmsCodeApi
{
    if (!_getSmsCodeApi) {
        _getSmsCodeApi = [PywGetSmsCodeApi manager];
    }
    return _getSmsCodeApi;
}

- (NSString *)getSmsType
{
    return self.smsType;
}

#pragma mark - Action
- (void)getValidCodeCilck
{
    [self endEditing:YES];
    
    // 判断输入手机号 是否合法
    NSString *phoneNum = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (phoneNum.length <= 0) {
        
        [PywMBProgressHUD showError:@"请填写手机号" toView:self];
    } else if (![[PywCommonManager manager] validatePhone:self.phoneTextView.text]){
        
        [PywMBProgressHUD showError:@"手机号码不合法！" toView:self];
    } else {
        [self requestGetSmsCode];
    }
    
}
#pragma mark - Request
/**
 *  获取验证码
 */
- (void)requestGetSmsCode
{
    WEAK_SELF;
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"4" actionSn:@"10" typeSn:@"3"];
    [self.getSmsCodeApi requestGetSmsCodeWithParams:[self requestCheckPhoneParams] success:^(NSString *status, id anyObjct) {
        // 跳转控制器
        if (weakSelf.getSmsCodeComplete) {
            weakSelf.getSmsCodeComplete(status,[weakSelf getSmsType],weakSelf.phoneTextView.text);
        }
        
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  验证密保手机  传入的参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestCheckPhoneParams
{
    NSString *phoneNumber = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:phoneNumber forKey:@"phone"];
    [params setObject:self.smsType forKey:@"smstype"];
    return params;
}


#pragma mark - PywAttributeTapActionDelegate
- (void)pyw_attributeTapReturnString:(NSString *)string range:(NSRange)range index:(NSInteger)index
{
    NSString *phone = @"4000709394";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]]];
}


@end
